<?php
namespace app\admin\controller;
use think\facade\{View,Session,Db};
use app\BaseController;

class Order extends Base
{

    public function orderlst()
    {
        if($this->Ajax){
            $page=input("page",1);
            $size = input("limit", 10);
            $mp=[];
            if(trim(input('umsg'))){
                $eo=[ ["mobile", "like","%".trim(input('umsg'))."%"] ];
                $mp[]=["iuser_id","in",Db::name("iuser")->where($eo)->column("id")];
            }//nickname|reaname|

                   // 时间区间查询
            $get=input('param.');
            if(!empty($get['time1']) && !empty($get['time2'])){
                if(trim($get['time1']) == trim($get['time2'])){
                    $get['time2']=  date("Y-m-d",strtotime('+1 days',strtotime(trim($get['time1']))));
                }
                $get['time1']= strtotime($get['time1']); //trim($get['time1']);
                $get['time2']= strtotime($get['time2']); //trim($get['time2']);
                $mp[]=['addtime','between',[trim($get['time1']),trim($get['time2'])]];
            }elseif(!empty($get['time1'])){
                $get['time1']=  trim($get['time1']);
                $mp[]=['addtime','>',strtotime($get['time1'])];
            }elseif(!empty($get['time2'])){
                $get['time2']=  trim($get['time2']);
                $mp[]=['addtime','<=',strtotime($get['time2'])];
            }
            $mp[]=['status',"=",1];
            if(strlen(input('is_staff'))){
                $mp[]=["is_tk","=",input('is_staff')];
            }
            if(trim(input('admin'))){
                 $mp[]=["admin","like","%".trim(input('admin'))."%"];
            }
            $arrs=["","A","B","C","D","E"];
            $lst=Db::name("order")->where($mp)->order("id desc")->page($page,$size)->select()->toArray();
            foreach($lst as &$v){
              
                $v['umsg']=Db::name("iuser")->find($v['iuser_id']);
                $v['addtime']=date("Y-m-d",$v['addtime']);
                $v['paytime']=$v['status'] ? date("Y-m-d H:i:s",$v['paytime']) : "-";
                $v['res_1']=$arrs[ json_decode($v['res_msg'],true)['xinlevel'] ];
                $v['res_2']=$v['res_2']."%";
                $v['tktime']=$v['is_tk']==1 ? date("Y-m-d H:i:s",$v['tk_time']) : "-";
            }
            $count= Db::name("order")->where($mp)->count();

           

            return resmsg(0,"检索成功",$lst, $count);
        }
        //初始化页面
        return View::fetch();
    }
    public function orderdetails(){
        $msg=Db::name("order")->find(input('id'));
        
        $arr=json_decode($msg['res_msg'],true);

        View::assign('msg',$msg);
        View::assign('arr',$arr);
   

        return View::fetch('order/src/orderdetails');
    }
    //退款订单
    public function ordertk(){
        $id=input("id");
        $msg=Db::name("order")->where("status",1)->find($id);
        if($msg['is_tk']==1){
            return returnmsg(199,"订单已退款，不可重复退款");
        }
        // 启动事务
        Db::startTrans();
        try {
            Db::name("order")->where("id",$id)->update([
                'is_tk'=>1,
                'tk_time'=>time()
                ]);
                  $refundNo="return_".uniqid()."_".$msg['iuser_id'];
                $wxmsg=Db::name("wxpay_log")->where("aut_id",$msg['id'])->where("status",1)->find();
                if($wxmsg['ut_type']==0){
                    //微信退款
                        $Wxrpay=new \app\api\service\Wxrpay(0);
                        $totalFee=$wxmsg['price'];
                        $refundFee=$wxmsg['price'];
                      
                        $res= $Wxrpay->doRefund($totalFee, $refundFee, $refundNo,"", $wxmsg['no'],"同意退款");
             
                }else{
                    //支付宝退款
                    $zhif=new \app\api\service\Alipay();
                    $zhif->repay($wxmsg['no'],$wxmsg['price']);
                    
                }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            
            return returnmsg(199,"数据错误,".$e->getMessage());
        }
        
        
        return returnmsg(200,"退款成功");
        
    }




   
}
